package models;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;

import model.wrap.node.Node;

import play.db.ebean.Model;

@Entity
@Table(name = "link")
public class Link extends Model
{
    private static final long serialVersionUID = 1L;
    @EmbeddedId
    LinkPK linkPK;

    protected static String ID_STRUCTURE = "id_structure";
    protected static String ID_PARENT = "id_parent";
    protected static String ID_CHILD = "id_child";

    public static Model.Finder<Long, Link> find = new Model.Finder<Long, Link>(
	    Long.class, Link.class);
    
    public Link()
    {
	this.linkPK = new LinkPK();
    }
    
    public static ArrayList<Node> getParents(Node child)
    {
	ArrayList<Node> list = new ArrayList<Node>();
	List<Link> links = Link.find.where().eq(Link.ID_CHILD, (long)child.getId()).findList();
	for (Link link : links)
	{
	    list.add(new Node(link.getId_parent()));
	}
	return list;
    }
    
    public static ArrayList<Node> getChild(Node parent)
    {
	ArrayList<Node> list = new ArrayList<Node>();
	List<Link> links = Link.find.where().eq(Link.ID_PARENT, (long)parent.getId()).findList();
	for (Link link : links)
	{
	    list.add(new Node(link.getId_child()));
	}
	return list;
    }
    
    public static ArrayList<Link> getAllLinks(Node node)
    {
	List<Link> links;
	ArrayList<Link> result = new ArrayList<Link>();
	
	links = Link.find.where().eq(Link.ID_PARENT, (long)node.getId()).findList();
	for (Link link : links)
	{
	    result.add(link);
	}
	links = Link.find.where().eq(Link.ID_CHILD, (long)node.getId()).findList();
	for (Link link : links)
	{
	    result.add(link);
	}
	return result;
    }
    
    public long getId_structure()
    {
	return linkPK.id_structure;
    }

    public void setId_structure(long id_structure)
    {
	this.linkPK.id_structure = id_structure;
    }

    public long getId_parent()
    {
	return linkPK.id_parent;
    }

    public void setId_parent(long id_parent)
    {
	this.linkPK.id_parent = id_parent;
    }

    public long getId_child()
    {
	return linkPK.id_child;
    }

    public void setId_child(long id_child)
    {
	this.linkPK.id_child = id_child;
    }
}
